const crateStore = function (reducer, initState) {
  let state = initState;
  let listeners = [];
  function subscribe(handler) {
    listeners.push(handler);
  }
  function dispatch(action) {
    const currentState = reducer(state, action);
    state = currentState;
    listeners.forEach((handler) => {
      handler();
    });
  }
  function getState() {
    return state;
  }
  return {
    subscribe,
    dispatch,
    getState,
  };
};

//  const redoucer = combineReducer({
//     a:AReducer,
//     b:BReducer
//  })
// const state = {
//     a:xxx,
//     b:xxx
// }
const combineReducer = (reducers) => {
  const keys = Object.keys(reducers);
  return function (state = {}, action) {
    const nextState = {};
    keys.forEach((key) => {
      const reducer = reducers[key];
      const next = reducer(state[key], action);
      nextState[key] = next;
    });
    return nextState;
  };
};
